<style>

</style>

<template>
  <p>
    <a-space>
        <a-button type="primary" @click="handleQuery()">刷新</a-button>
    </a-space>
    </p>
    <a-table 
        :dataSource="skTokens" 
        :columns="columns" 
        :pagination="pagination" 
        :loading="loading"
        @change="handleTableChange" 
    >
        <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'operation'" >
                <a-space>
                    <a @click="onEdit(record)">修改令牌余量</a>
                </a-space>
            </template>
            <template v-else-if="column.dataIndex === 'status'">
                <span v-for="item in CONFIRM_ORDER_STATUS_ARRAY" :key="item.code">
                    <span v-if="item.code === record.status">
                        {{item.desc}}
                    </span>
                </span>
            </template>
        </template>
    </a-table>

    <a-modal v-model:visible="visible" title="秒杀令牌" @ok="handleOk" ok-text="确认" cancel-text="取消">
        <a-form :model="skToken" :label-col="{span: 4}" :wrapper-col="{span: 14}">
            <a-form-item label="余票数量">
                <a-input v-model:value="skToken.count"/>
            </a-form-item>
        </a-form>
    </a-modal>
</template>

<script>
import { defineComponent,ref,onMounted} from 'vue';
import { notification } from 'ant-design-vue';
import axios from 'axios';

export default defineComponent({
    name: " skToken-view",
    components: {  },
    setup() {
        // 展示模态窗的标签的定义
        const visible = ref(false);
        const loading = ref(false);
        // 定义秒杀令牌对象
        let skToken = ref({
            id: undefined,
            date:undefined,
            trainCode: undefined,
            count: undefined,
            createTime: undefined,
            updateTime: undefined
        });
        const skTokens = ref([]);
        const columns = [
            {
                title: '日期',
                dataIndex: 'date',
                key: 'date',
            },
            {
                title: '车次编号',
                dataIndex: 'trainCode',
                key: 'trainCode',
            },
            {
                title: '令牌余量',
                dataIndex: 'count',
                key: 'count',
            },
            {
                title: '操作',
                dataIndex: 'operation',
                key: 'operation',
            }
        ];
        let pagination = ref({
            total: 0,
            current: 1,
            pageSize: 10,
        });

        // 点击编辑展示模态窗的函数方法
        const onEdit = (record) => {
            console.log("edit");
            skToken.value = window.Tool.copy(record);
            visible.value = true;
        };

        // 点击模态窗的ok按钮执行的函数方法
        const handleOk = e => {
            switch (skToken.value.type) {
                case "高铁":
                    skToken.value.type = "G"
                    break;
                case "快速":
                    skToken.value.type = "K"
                    break;
                case "动车":
                    skToken.value.type = "D"
                    break;
                default:
                    break;
            }
            axios.post("/business/admin/sk-token/save", skToken.value)
                .then(response => {
                let respData = response.data;
                console.log(respData);
                if (respData.data) {
                    notification.success({ description: '保存成功！' });
                    // 设置为false，关闭模态窗
                    visible.value = false;
                    // 调用列表查询的接口，刷新数据
                    handleQuery();
                }
                else {
                    notification.error({ description: respData.errorMessage });
                }
            })
                .catch(error => {
                let errData = error.response.data;
                console.log(errData);
                notification.error({ description: errData.data });
            });
        };

        // 车站信息列表查询的方法
        const handleQuery = e => {
            // 刷新时候不带参数的判断
            if (!e) {
                e = {
                    page: 1,
                    limit: pagination.value.pageSize
                };
            }
            // 开启loading效果
            loading.value = true;
            axios.get("/business/admin/sk-token/query", {
                params: {
                    page: e.page,
                    limit: e.limit,
                }
            })
                .then(response => {
                // 关闭loading效果
                loading.value = false;
                let respData = response.data;
                let skTokenDataList = respData.data.dataList;
                // 分页控件的设置
                let total = respData.data.total;
                let currentPage = respData.data.currentPage;
                let pageSize = respData.data.pageSize;
                skTokens.value = skTokenDataList;
                pagination.value.current = currentPage;
                pagination.value.pageSize = pageSize;
                pagination.value.total = total;
            })
                .catch(error => {
                // 关闭loading效果
                loading.value = false;
                let errData = error.response.data;
                console.log(errData);
                notification.error({ description: errData.data });
            });
        };

        // 页码的点击事件
        const handleTableChange = (pagination) => {
            console.log("pagination:", pagination.value);
            // 调用车站列表查询方法
            handleQuery({
                page: pagination.current,
                limit: pagination.pageSize
            });
        };

        const onChangeCode = (train) => {
            console.log("车次下拉组件选择：", train);
            let t = window.Tool.copy(train);
            delete t.id;
            // 用assign可以合并
            skToken.value = Object.assign(skToken.value, t);
        };

        // 钩子函数，页面初始化的时候执行
        onMounted(() => {
            handleQuery({
                page: pagination.value.current,
                limit: pagination.value.pageSize
            });
        });
        return {
            visible,
            loading,
            skToken,
            skTokens,
            columns,
            pagination,
            onEdit,
            handleOk,
            handleQuery,
            handleTableChange,
            onChangeCode
        };
    },
});
</script>